import datetime


class Bill:
    def __init__(self, date, amount, category, note):
        self.date = date
        self.amount = amount
        self.category = category
        self.note = note


class BillManager:
    def __init__(self):
        self.incomes = []
        self.expenses = []

    def record_income(self, amount, category, note):
        if amount <= 0:
            print("收入金额必须为正数。")
            return
        bill = Bill(datetime.datetime.now().strftime("%Y-%m-%d"), amount, category, note)
        self.incomes.append(bill)
        print("收入记录成功！")

    def record_expense(self, amount, category, note):
        if amount <= 0:
            print("支出金额必须为正数。")
            return
        bill = Bill(datetime.datetime.now().strftime("%Y-%m-%d"), amount, category, note)
        self.expenses.append(bill)
        print("支出记录成功！")

    def query_records(self, record_type):
        records = self.incomes if record_type == 'income' else self.expenses
        if not records:
            print(f"没有{record_type}记录。")
            return
        for record in records:
            print(f"日期: {record.date}, 金额: {record.amount}, 类别: {record.category}, 备注: {record.note}")

    def statistics(self):
        total_income = sum(record.amount for record in self.incomes)
        total_expense = sum(record.amount for record in self.expenses)
        print(f"总收入: {total_income}")
        print(f"总支出: {total_expense}")
        print(f"结余: {total_income - total_expense}")

    def display_menu(self):
        print("\n个人账单管理系统")
        print("1. 记录收入")
        print("2. 记录支出")
        print("3. 查询收入记录")
        print("4. 查询支出记录")
        print("5. 统计总收入和总支出")
        print("6. 退出")


def main():
    manager = BillManager()
    while True:
        manager.display_menu()
        choice = input("请选择操作: ")

        if choice == '1':
            amount = float(input("请输入收入金额: "))
            category = input("请输入收入类别: ")
            note = input("请输入备注 (可选): ")
            manager.record_income(amount, category, note)
        elif choice == '2':
            amount = float(input("请输入支出金额: "))
            category = input("请输入支出类别: ")
            note = input("请输入备注 (可选): ")
            manager.record_expense(amount, category, note)
        elif choice == '3':
            manager.query_records('income')
        elif choice == '4':
            manager.query_records('expense')
        elif choice == '5':
            manager.statistics()
        elif choice == '6':
            print("退出系统。")
            break
        else:
            print("无效选择，请重新选择。")


if __name__ == "__main__":
    main()